<!-- 发送用户 -->

<template>
  <div>
    <FormSlot label="接收人" bind="sendUserDtos" tip="请选择接收人" required>
      <div v-if="one.sendUserDtos.length">
        <BtnText v-for="item in one.sendUserDtos" :key="item.uaaId" @click="toOpen">
          <span>{{ item.name }}{{ item.phone ? ` (${item.phone})` : '' }}</span>
        </BtnText>
        <BtnText v-if="!isShow" title="清除" @click="toClear" />
      </div>
      <BtnPrimary v-else-if="!isShow" title="选择" @click="toOpen" />
      <!-- <BtnPrimary v-if="!isShow" title="选择" @click="toOpen" /> -->
    </FormSlot>

    <!-- <FormSlot>
      <WebTable :list="one.sendUserDtos" noPage>
        <RowIndex />
        <RowText label="用户名称" bind="name" />
        <RowText label="联系方式" bind="phone" :minWidth="100" />
        <RowTree :label="`所属${CORE.MAP.ORGAN}`" bind="organId" :dict="CORE.ORGAN.DICT" :minWidth="120" />
        <RowOperate v-if="!isShow" :btns="['del']" onDel="toDel" :width="80" />
      </WebTable>
    </FormSlot> -->

    <EasyModal v-model="easyVisible" title="选择接收人" large @submit="toSubmit">
      <ChooseUser :obj="obj" bind="user" hasMobile />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'SendUser',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { user: [] }, // 数据对象
    }
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj, one } = this
      one.sendUserDtos = obj.user.map((item, i) => ({
        index: i + 1,
        uaaId: item.uaaUserId,
        name: item.name,
        phone: item.phone,
        organId: item.organId[0] || '',
      }))
      one.sendUsers = one.sendUserDtos.map((item) => item.uaaId)
      this.easyVisible = false
    },

    /**
     * 重置
     */
    toReset() {
      const user = this.one.sendUserDtos.map((item) => ({
        uaaUserId: item.uaaId,
        name: item.name,
        phone: item.phone,
        organId: item.organId.split(','),
      }))
      this.obj = { user }
    },

    /**
     * 清除
     */
    toClear() {
      this.one.sendUserDtos = []
      this.one.sendUsers = []
      this.obj = { user: [] }
    },

    /**
     * 删除
     */
    toDel(row) {
      const { one } = this
      const index = one.sendUserDtos.findIndex((item) => item.uaaId === row.uaaId)
      one.sendUserDtos.splice(index, 1)
      one.sendUsers = one.sendUserDtos.map((item) => item.uaaId)
    },
  },
}
</script>
